﻿Imports System
Imports System.IO
Imports System.Data.OleDb
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Imaging
Namespace Arwani
    Partial Class Backend_AddNewExhibition
        Inherits System.Web.UI.Page

        Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
            Dim strQ As String
            Dim arwCon As New ArwaniController
            Dim dateFrom As String = CDate(Me.txtDateFrom.Text).ToString("yyyy-M-dd")
            Dim dateTo As String = CDate(Me.txtDateTo.Text).ToString("yyyy-M-dd")
            If Request.QueryString("exbid") <> 0 Then
                strQ = "update exhibitions set sortorder=" & CInt(Me.txtSortOrder.Text) & ", catid=" & Me.cboCategory.SelectedItem.Value & ", title='" & Me.txtTitle.Text.Replace("'", "''") & "',description='" & Me.txtDesc.Text.Replace("'", "''") & "',datefrom='" & dateFrom & "',dateto='" & dateTo & "' where id=" & Val(Request.QueryString("exbid"))
                arwCon.ExecuteNonQuery(strQ)
            Else

                strQ = "insert into exhibitions (title,description,	datefrom,dateTo,catid,sortorder) values ('" & Me.txtTitle.Text.Replace("'", "''") & "','" & Me.txtDesc.Text.Replace("'", "''") & "','" & dateFrom & "','" & dateTo & "'," & Me.cboCategory.SelectedItem.Value & "," & CInt(Me.txtSortOrder.Text) & ")"
                arwCon.ExecuteNonQuery(strQ)
            End If
            Response.Redirect("ManageExhibition.aspx")
        End Sub
        Public Function getImage(ByVal imgSrc As String) As String
            Dim strReturn As String = "images/main_banner.png"
            If File.Exists(Server.MapPath("../Slider/" & imgSrc)) Then
                strReturn = "../Slider/" & imgSrc
            End If
            Return strReturn
        End Function
        Protected Sub cmdBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdBack.Click
            Response.Redirect("ManageExhibition.aspx")
        End Sub
        Sub ResizeImages(ByVal FileName, ByVal NewFileName)
            Dim intWidth, intHeight As Integer

            intWidth = 730 '*** Fix Width ***'  
            intHeight = 248   '*** If = 0 Auto Re-Cal Size ***'  

            Dim objGraphic As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("../Slider/" & FileName))

            Dim objBitmap As Bitmap
            '*** Calculate Height ***'  
            If intHeight > 0 Then
                objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
            Else
                If objGraphic.Width > intWidth Then
                    Dim ratio As Double = objGraphic.Height / objGraphic.Width
                    intHeight = ratio * intWidth
                    objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
                Else
                    objBitmap = New Bitmap(objGraphic)
                End If
            End If

            '*** Save As  ***'  
            objBitmap.Save(Server.MapPath("../Slider/" & NewFileName), objGraphic.RawFormat)

            '*** Close ***'  
            objGraphic.Dispose()

            '*** Nothing ***'  
            objBitmap = Nothing
            objGraphic = Nothing

        End Sub
        Public Sub loadCategory()

            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from exhibitions_category"
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Me.cboCategory.DataTextField = "catname"
            Me.cboCategory.DataValueField = "catid"
            Me.cboCategory.DataSource = dtTable
            Me.cboCategory.DataBind()

        End Sub
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            If (Session("ADMIN_USER") IsNot Nothing) Then
                If Page.IsPostBack Then Exit Sub
                Call loadCategory()
                If Request.QueryString("exbid") <> 0 Then Call loadData(Val(Request.QueryString("exbid")))

            Else
                Response.Redirect("Default.aspx")
            End If

        End Sub
        Public Sub loadData(ByVal exID As Integer)
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select id,sortorder,title,description,CAST(datefrom as char) as datefrom,CAST(dateto as char) as dateto,catid from exhibitions where id=" & CInt(Request.QueryString("exbid"))
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            If dtTable.Rows.Count > 0 Then
                If Not IsDBNull(dtTable.Rows(0)("title")) Then Me.txtTitle.Text = dtTable.Rows(0)("title")
                If Not IsDBNull(dtTable.Rows(0)("description")) Then Me.txtDesc.Text = dtTable.Rows(0)("description")
                If Not IsDBNull(dtTable.Rows(0)("dateFrom")) Then Me.txtDateFrom.Text = dtTable.Rows(0)("dateFrom")
                If Not IsDBNull(dtTable.Rows(0)("dateTo")) Then Me.txtDateTo.Text = dtTable.Rows(0)("dateTo")
                If Not IsDBNull(dtTable.Rows(0)("sortorder")) Then Me.txtSortOrder.Text = dtTable.Rows(0)("sortorder")
                Try
                    If Not IsDBNull(dtTable.Rows(0)("catid")) Then cboCategory.Items.FindByValue(CInt(dtTable.Rows(0)("catid").ToString())).Selected = True
                Catch ex As Exception

                End Try
            End If
        End Sub
    End Class
End Namespace